package com.caregiver.watch.business.user.service.impl;

import com.caregiver.watch.business.user.service.AuthService;
import com.caregiver.watch.common.enums.SendSmsCodeEnum;
import com.caregiver.watch.common.exception.BaseException;
import com.caregiver.watch.common.exception.business.BusinessException;
import com.caregiver.watch.common.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import static com.caregiver.watch.common.constant.Constants.COLON;

/**
 * @Description: 鉴权服务
 * @Author 疆戟
 * @Date 2025/8/16 11:05
 * @Version 1.0
 */
@Service
@Slf4j
public class AuthServiceImpl implements AuthService {


    @Autowired
    private RedisService redisService;

    @Override
    public void validateSmsCode(String phone, String code, SendSmsCodeEnum sendSmsCodeType) {
        String oldCode = redisService.getCacheString(sendSmsCodeType.getKey() + COLON + phone);
        if (StringUtils.isBlank(oldCode)) {
            throw new BusinessException("验证码过期");
        } else if (!StringUtils.equals(oldCode, code)) {
            throw new BusinessException("验证码错误");
        }
    }
}
